#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "open62541.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


static UA_StatusCode
helloWorldMethodCallback(UA_Server *server,
                         const UA_NodeId *sessionId, void *sessionHandle,
                         const UA_NodeId *methodId, void *methodContext,
                         const UA_NodeId *objectId, void *objectContext,
                         size_t inputSize, const UA_Variant *input,
                         size_t outputSize, UA_Variant *output) {
    UA_String *inputStr = (UA_String*)input->data;

    UA_String tmp = UA_STRING_ALLOC("Hello ");
    if(inputStr->length > 0) {
        tmp.data = (UA_Byte *)UA_realloc(tmp.data, tmp.length + inputStr->length);
        memcpy(&tmp.data[tmp.length], inputStr->data, inputStr->length);
        tmp.length += inputStr->length;
    }

    printf("server:inputStr->length = %i\n",inputStr->length);

    printf("Server received: %.*s\n", (int)inputStr->length, inputStr->data);

    UA_String argString = UA_STRING("Hello Client,I am Server");
    UA_Variant_init(output);


    UA_Variant_setScalarCopy(output, &argString, &UA_TYPES[UA_TYPES_STRING]);

    //UA_Variant_setScalarCopy(output, &tmp, &UA_TYPES[UA_TYPES_STRING]);


    UA_String_clear(&tmp);
    UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Hello World was called",helloWorldMethodCallback);
    return UA_STATUSCODE_GOOD;
}

void MainWindow::on_pushButton_clicked()
{

    // 绑定成员函数，固定第一个参数为对象指针，第二个参数为3



    Open62541Server *server = new Open62541Server("MyInput","MyOutput",helloWorldMethodCallback);
    server->start();

}

